package decorationPrice.decorator;

import decorationPrice.abs.BaseFruit;
import decorationPrice.abs.Fruit;

public class DiscountDecorator extends BaseFruit implements Fruit {
    private Fruit fruit;
    private double discountRate; // 折扣率，如0.8表示8折

    public DiscountDecorator(Fruit fruit, double discountRate) {
        super(fruit.getName(),fruit.getPrice(),fruit.getWeight());
        this.fruit = fruit;
        this.discountRate = discountRate;
    }

    @Override
    public double calculateCost() {
        return fruit.calculateCost() * discountRate;
    }

    @Override
    public String getName() {
        double discountPercent = (1 - discountRate) * 100;
        // 使用String.format保留合适的小数位数
        return fruit.getName() + "(折扣" + String.format("%.1f", discountPercent).replaceAll("0*$", "").replaceAll("\\.$", "") + "%)";
    }
}
